package com.oscar.ehome.view.pageview;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.XiaoXiFinalBean;
import com.oscar.ehome.bean.pagebean.XiaoXiXiangQingBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.DateUtil;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.messageview.MessageDetilActivity;
import com.oscar.ehome.view.pageview.adapter.MessageFinalAdapter;
import com.oscar.ehome.view.pageview.adapter.MessageYiduAdapter;
import com.oscar.ehome.view.pageview.view.xiaoxiFinalView;
import com.oscar.ehome.view.teamview.TeamDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/12/7
 */

public class xiaoxifinalActivity extends BasesActivity<xiaoxiFinalView, PagePresenter> implements xiaoxiFinalView, XRefreshView.XRefreshViewListener {
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.img_spinner)
    ImageView imgSpinner;
    @BindView(R.id.liear_title)
    LinearLayout liearTitle;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRightNew;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.message_img)
    ImageView message_img;
    @BindView(R.id.message_img_layout)
    RelativeLayout messageImgLayout;
    @BindView(R.id.message_title)
    TextView messageTitle;
    @BindView(R.id.message_titles)
    TextView message_titles;
    @BindView(R.id.message_refview)
    XRefreshView messageRefview;
    @BindView(R.id.yiduliebiao)
    LinearLayout yiduliebiao;
    @BindView(R.id.weiduliebiao)
    LinearLayout weiduliebiao;
    @BindView(R.id.message_listview_weidu)
    MyList message_listview_weidu;
    @BindView(R.id.message_listview_yidu)
    MyList message_listview_yidu;
    @BindView(R.id.fasongnumber)
    TextView fasongnumber;
    @BindView(R.id.weidurenshu)
    TextView weidurenshu;
    @BindView(R.id.yidurenshu)
    TextView yidurenshu;
    @BindView(R.id.message_title_layout)
    RelativeLayout messageTitleLayout;
    @BindView(R.id.message_time_text)

    TextView messageTimeText;
    @BindView(R.id.viewone)
    View viewone;
    @BindView(R.id.viewtwo)
    View viewtwo;
    String token = "";
    XiaoXiXiangQingBean.liebiaoshuju bean;
    private PagePresenter pagePresenter;
    MessageFinalAdapter adapter;
    MessageYiduAdapter adapteryidu;
    @BindView(R.id.weidutupian)
    ImageView weidutupian;

    @BindView(R.id.xiangqingtiaozhuan)
    LinearLayout xiangqingtiaozhuan;
    public static long lastRefreshTime;
    @BindView(R.id.yidutupian)
    ImageView yidutupian;
    private int page = 1;//页数
    List<XiaoXiFinalBean.ReadUserList> list_weidu = new ArrayList<>();
    List<XiaoXiFinalBean.ReadUserList> list_yidu = new ArrayList<>();
    int weiduxianshi = 0, yiduxianshi = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messagereader_list);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(xiaoxifinalActivity.this).getSystemConfig().getColor()));

        initView();

        initData();
    }

    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("消息统计");
        token = SystemConfigFactory.getInstance(xiaoxifinalActivity.this).getSystemConfig().getToken();

        bean = (XiaoXiXiangQingBean.liebiaoshuju) getIntent().getSerializableExtra("id");

        titleImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        messageRefview.setXRefreshViewListener(this);
        // 设置是否可以下拉刷新
        messageRefview.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        messageRefview.setPullLoadEnable(false);
        // 设置上次刷新的时间
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        messageRefview.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        messageRefview.setAutoRefresh(false);
    }

    public void initData() {
        showLoading();
        messageTitle.setText(bean.getTitle());
        fasongnumber.setText("发送：" + bean.getAllCanReadNum() + "人");

        String date = DateUtil.timeLogic(bean.getUpdateDate());
        messageTimeText.setText(date);

        String type = bean.getType();
        switch (type) {
            case "1"://通知
                message_img.setImageResource(R.mipmap.tongzhi1);
                message_titles.setText(R.string.message_inform);
                break;
            case "2"://激励
                message_img.setImageResource(R.mipmap.jili1);
                message_titles.setText(R.string.message_exciting);
                break;
            case "3"://产品
                message_img.setImageResource(R.mipmap.chanpin1);
                message_titles.setText(R.string.message_product);
                break;
            case "4"://会议
                message_img.setImageResource(R.mipmap.huiyi1);
                message_titles.setText(R.string.message_meeting);
                break;
            case "5"://系统
                message_img.setImageResource(R.mipmap.xitong1);
                message_titles.setText(R.string.message_system);
                break;
        }
        if (isNetWork()) {
            if (bean != null) {
                String s = String.valueOf(page);
                pagePresenter.xiaoxifinal(token, "20", s, bean.getId());
            }

        } else {

        }

        weiduliebiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (weiduxianshi == 0) {
                    weiduxianshi = 1;
                    message_listview_weidu.setVisibility(View.VISIBLE);
                    weidutupian.setImageResource(R.mipmap.xiajiantou);
                    if (list_weidu.size() > 0) {
                        viewone.setVisibility(View.VISIBLE);
                    }

                } else {
                    viewone.setVisibility(View.GONE);
                    message_listview_weidu.setVisibility(View.GONE);
                    weiduxianshi = 0;
                    weidutupian.setImageResource(R.mipmap.youjian);
                }
            }
        });

        yiduliebiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yiduxianshi == 0) {
                    yiduxianshi = 1;
                    message_listview_yidu.setVisibility(View.VISIBLE);
                    yidutupian.setImageResource(R.mipmap.xiajiantou);
                    if (list_yidu.size() > 0) {
                        viewtwo.setVisibility(View.VISIBLE);
                    }
                } else {
                    viewtwo.setVisibility(View.GONE);
                    message_listview_yidu.setVisibility(View.GONE);
                    yiduxianshi = 0;
                    yidutupian.setImageResource(R.mipmap.youjian);
                }
            }
        });
        xiangqingtiaozhuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(xiaoxifinalActivity.this, MessageDetilActivity.class);
                intent.putExtra("ids", bean.getId());
                intent.putExtra("type", bean.getType());
                CommonUtils.pop = 2;
                startActivity(intent);


            }
        });
        message_listview_weidu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(xiaoxifinalActivity.this, TeamDetailActivity.class);
                intent.putExtra("id", list_weidu.get(position).getId());
                intent.putExtra("set", 2);
                startActivity(intent);
            }
        });
        message_listview_yidu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(xiaoxifinalActivity.this, TeamDetailActivity.class);
                intent.putExtra("id", list_yidu.get(position).getId());
                intent.putExtra("set", 2);
                startActivity(intent);
            }
        });
    }


    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }


    @Override
    public void getxiaoxifinalMessage(XiaoXiFinalBean result) {
        messageRefview.stopRefresh();
        if (result != null && !result.equals("")) {
            weidurenshu.setText(result.getUnreadUserList().size() + "");
            yidurenshu.setText(result.getReadUserList().size() + "");
            list_weidu = result.getUnreadUserList();
            adapter = new MessageFinalAdapter(this, list_weidu);
            message_listview_weidu.setAdapter(adapter);
            message_listview_yidu.setVisibility(View.VISIBLE);
            list_yidu = result.getReadUserList();
            adapteryidu = new MessageYiduAdapter(this, list_yidu);
            message_listview_yidu.setAdapter(adapteryidu);
            if (list_yidu.size() > 0) {
                viewtwo.setVisibility(View.VISIBLE);
            }


            dismissLoading();
        } else {
            dismissLoading();
        }
    }

    @Override
    public void erroy() {
        messageRefview.stopRefresh();
    }

    @Override
    public void erry(String result) {
        messageRefview.stopRefresh();
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {
        lastRefreshTime = messageRefview.getLastRefreshTime();
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        page = 1;
        String s = String.valueOf(page);
        pagePresenter.xiaoxifinal(token, "20", s, bean.getId());
        showLoading();
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        page++;
        String s = String.valueOf(page);

        pagePresenter.xiaoxifinal(token, "20", s, bean.getId());
        showLoading();
    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }
}
